package com.shell.governor.security.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.entity.auth.AuthUser;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.VerifyCodeUtil;
import com.shell.governor.security.user.dao.PubSecurityIssueResultDao;
import com.shell.governor.security.user.po.PubSecurityIssueResult;
import com.shell.governor.security.user.service.PubSecurityIssueResultService;
import com.shell.governor.security.user.util.SecurityCodeUtil;
import com.shell.governor.user.dao.PubUserDao;
import com.shell.governor.user.vo.PubUserVo;
import com.shell.governor.user.vo.UserQueryVo;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class PubSecurityIssueResultServiceImpl implements PubSecurityIssueResultService {
	@Autowired
	private PubSecurityIssueResultDao pubSecurityIssueResultDao;
	@Autowired
	private PubUserDao pubUserDao;

	public RetMsg editSecurityIssueResult(PubSecurityIssueResult pIssueResult) {
		PubSecurityIssueResult pIssueResultQuery = new PubSecurityIssueResult();
		pIssueResultQuery.setIssueId(pIssueResult.getIssueId());
		pIssueResultQuery.setUserId(CommonDataUtil.getCurUser().getUserId());
		PubSecurityIssueResult result = (PubSecurityIssueResult) this.pubSecurityIssueResultDao
				.selectOne(pIssueResultQuery);
		if (result != null) {
			pIssueResult.setUserId(CommonDataUtil.getCurUser().getUserId());
			this.pubSecurityIssueResultDao.updateByPrimaryKey(pIssueResult);
			return new RetMsg("success", "��������!");
		}
		if (StringUtil.isNotEmpty(pIssueResult.getIssueResult())) {
			pIssueResultQuery.setIssueResult(pIssueResult.getIssueResult());
		}
		this.pubSecurityIssueResultDao.insert(pIssueResultQuery);
		return new RetMsg("success", "��������!");
	}

	public RetMsg checkSecurityIssue(String loginName, String questionId, String answer, String verificationCode) {
		RetMsg msg = VerifyCodeUtil.checkVerifyCode(verificationCode);
		if ("success" != msg.getType()) {
			return msg;
		}
		UserQueryVo user = new UserQueryVo();
		user.setLoginName(loginName);
		String[] notState = { "D" };
		user.setNotStates(notState);
		List<PubUserVo> users = this.pubUserDao.queryUser(user);
		if ((users == null) || (users.size() == 0)) {
			return new RetMsg("error", "����������");
		}
		PubSecurityIssueResult result = new PubSecurityIssueResult();
		result.setIssueId(questionId);
		result.setUserId(((PubUserVo) users.get(0)).getUserId());
		result = (PubSecurityIssueResult) this.pubSecurityIssueResultDao.selectOne(result);
		if (result == null) {
			return new RetMsg("error", "����������������");
		}
		if (answer.equals(result.getIssueResult())) {
			String srecurityCode = SecurityCodeUtil.setCode();
			RetMsg retMsg = new RetMsg("success", true, "��������");
			Map<String, String> m = new HashMap<String, String>();
			m.put("srecurityCode", srecurityCode);
			retMsg.setBean(m);

			return retMsg;
		}
		return new RetMsg("error", "����������������");
	}

	public PubSecurityIssueResult findSecurityIssueResult(String issueId) {
		AuthUser user = CommonDataUtil.getCurUser();
		PubSecurityIssueResult psir = new PubSecurityIssueResult();
		psir.setIssueId(issueId);
		psir.setUserId(user.getUserId());
		psir = (PubSecurityIssueResult) this.pubSecurityIssueResultDao.selectOne(psir);
		return psir;
	}
}
